package com.example.codegen.controller;

import com.example.codegen.model.Column;
import com.example.codegen.model.Datasource;
import com.example.codegen.model.Table;
import com.example.codegen.service.DatasourceService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/datasource")
@Tag(name = "数据源")
public class DatasourceController {

    private DatasourceService datasourceService;

    @Autowired
    public DatasourceController(DatasourceService datasourceService) {
        this.datasourceService = datasourceService;
    }

    @GetMapping("/list")
    @Operation(summary = "数据源列表")
    public List<Datasource> getDatasourceVersion(HttpServletRequest request) {
        log.info("Authorization: {}", request.getHeader("Authorization"));
        return datasourceService.getDatasourceList();
    }

    @PostMapping("/version")
    @Operation(summary = "获取数据库版本信息")
    public String getVersion(@RequestBody Datasource datasource) {
        return datasourceService.getVersion(datasource);
    }

    @PostMapping("/tables")
    @Operation(summary = "获取指定数据源下的数据表")
    public List<Table> getTables(@RequestBody Datasource datasource) {
        return datasourceService.getTables(datasource);
    }

    @PostMapping("/columns")
    @Operation(summary = "获取指定数据表的字段列表")
    public List<Column> getColumns(@RequestBody Datasource datasource, @RequestParam String tableName) {
        return datasourceService.getColumns(datasource, tableName);
    }
}
